package ${basePackage}.service.export.${module};

<#if !exportSame>import java.util.Date;</#if>
import java.util.List;

import org.apache.ibatis.session.ResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.unswift.annotation.api.Api;
import com.unswift.annotation.api.ApiField;
import com.unswift.annotation.api.ApiMethod;
import ${basePackage}.adapter.logger.LoggerExportTaskAdapter;
import ${basePackage}.adapter.${module}.${entityName}Adapter;
import ${basePackage}.enums.export.task.ExportTaskEventEnum;
import ${basePackage}.excel.IExcelTranslate;
import ${basePackage}.export.IExportBusinessService;
import ${basePackage}.pojo.bo.${module}.${currModule}.${entityName}ExportBo;
import ${basePackage}.pojo.bo.${module}.${currModule}.${entityName}ExportStopBo;
import ${basePackage}.pojo.dao.logger.export.task.LoggerExportTaskInsertDo;
import ${basePackage}.pojo.dao.${module}.${currModule}.${entityName}BigDataDo;
import ${basePackage}.pojo.dao.${module}.${currModule}.${entityName}DataDo;
import ${basePackage}.pojo.mo.${module}.${currModule}.${entityName}ExportMo;
import ${basePackage}.pojo.mso.logger.export.task.LoggerExportTaskQueueMso;
import ${basePackage}.pojo.vo.${module}.${currModule}.${entityName}ExportVo;
import ${basePackage}.pojo.vo.${module}.${currModule}.${entityName}ExportStopVo;
import ${basePackage}.rabbit.RabbitConstant;
<#if !exportSame>import ${basePackage}.rabbit.RabbitQueue;</#if>
import ${basePackage}.service.BaseService;
import ${basePackage}.sql.${module}.${currModule}.${entityName}Sql;
import com.unswift.utils.ExceptionUtils;
import com.unswift.utils.JsonUtils;
import com.unswift.utils.ObjectUtils;

@Service
@Api(value="${tableComment!''}导出服务", author="${username}", date="${date}", version="1.0.0")
public class ${entityName}ExportService extends BaseService implements IExportBusinessService<${entityName}ExportBo, ${entityName}DataDo, ${entityName}ExportMo>, IExcelTranslate, RabbitConstant{
	
	@Autowired
	@ApiField("${tableComment!''}公共服务")
	private ${entityName}Adapter ${entitySampleName}Adapter;
	
	@Autowired
	@ApiField("${tableComment!''}自定义Sql")
	private ${entityName}Sql ${entitySampleName}Sql;
	
	@Autowired
	@ApiField("导出任务服务")
	private LoggerExportTaskAdapter loggerExportTaskAdapter;
	<#if !exportSame>
	
	@Autowired
	@ApiField("Rabbit Mq发送消息服务")
	private RabbitQueue rabbitQueue;
	</#if>
	<#if !exportSame>
	
	@ApiMethod(value="根据条件导出${tableComment!''}数据", params=@ApiField("查询条件"))
	public ${entityName}ExportVo export(${entityName}ExportBo exportBo){
		ExceptionUtils.trueException(loggerExportTaskAdapter.existsTasking(this.getUserId(), ${entitySampleName}Adapter.getModule()), "task.is.currently.executing", "有${tableComment!''}导出");
		Date date=new Date();
		LoggerExportTaskInsertDo task=new LoggerExportTaskInsertDo();
		task.setName("${tableComment!''}");
		task.setModule(${entitySampleName}Adapter.getModule());
		task.setDocumentType("excel");
		task.setStatus((byte)0);
		task.setStartTime(date);
		task.setExportBeanClass(this.getClass().getName());
		task.setExportModelClass(${entityName}ExportBo.class.getName());
		task.setExportCondition(JsonUtils.toJson(exportBo));
		task.setResult((byte)0);
		int result=loggerExportTaskAdapter.save(task, false);//保存任务，不用加事务，防止队列消费的时候还没保存完数据
		rabbitQueue.sendMessage(EXPORT_EXCHANGE_NAME, EXPORT_ROUTE_KEY, new LoggerExportTaskQueueMso(task.getId(), ExportTaskEventEnum.START, this.getHeader(tokenConfig.getKeyName()), getLanguage()));//发送导出队列
		return new ${entityName}ExportVo(task.getId(), result);
	}
	
	@ApiMethod(value="停止导出${tableComment!''}", params=@ApiField("停止导出对象"))
	public ${entityName}ExportStopVo stop(${entityName}ExportStopBo exportStopBo){
		rabbitQueue.sendMessage(EXPORT_STOP_EXCHANGE_NAME, EXPORT_STOP_ROUTE_KEY, new LoggerExportTaskQueueMso(exportStopBo.getId(), ExportTaskEventEnum.STOP, this.getHeader(tokenConfig.getKeyName()), getLanguage()));//发送停止导出队列
		return new ${entityName}ExportStopVo(1);
	}
	</#if>
	
	@Override
	@ApiMethod(value="获取导出模板的类类型", returns=@ApiField("导出模板类类型"))
	public Class<${entityName}ExportMo> getModule(){
		return ${entityName}ExportMo.class;
	}
	
	@Override
	@ApiMethod(value="根据条件查询${tableComment!''}数量", params=@ApiField("查询条件"))
	public int findExportCount(${entityName}ExportBo exportBo){
		${entityName}BigDataDo search=this.convertPojo(exportBo, ${entityName}BigDataDo.class);//将Bo转换为So
		return ${entitySampleName}Adapter.findCount(search);
	}
	
	@Override
	@ApiMethod(value="查询导出的数据", params=@ApiField("查询条件"), returns=@ApiField("导出的数据游标对象，支持大数据"))
	public void findExportData(${entityName}ExportBo exportBo, ResultHandler<${entityName}DataDo> out){
		${entityName}BigDataDo search=this.convertPojo(exportBo, ${entityName}BigDataDo.class);//将Bo转换为So
		${entitySampleName}Adapter.findBigData(search, out);
	}
	
	@Override
	@ApiMethod(value="MyBatis流式查询读取的过程数据列表，开发者可对此数据加工", params=@ApiField("过程数据列表"))
	public void rowDataHandle(List<${entityName}DataDo> exportDataList){
		
	}
	
	@Override
	@ApiMethod(value="ExcelColumn注解中translateBean的翻译方法", params={@ApiField("数据字典类型"), @ApiField("当前语言"), @ApiField("当前值")})
	public String translate(String type, String language, Object key) {
		if(ObjectUtils.isNull(key)){
			return null;
		}
		if(ObjectUtils.isEmpty(key)){
			return key.toString();
		}
		return cacheAdapter.findDictionaryValueByKey(type, language, key.toString());//翻译逻辑，默认返回自己
	}
}